package gl.android.biyiqing.fragment;

import gl.API;
import gl.android.biyiqing.R;
import gl.android.biyiqing.bean.BYQUserInfo;
import gl.common.UserManger;
import gl.uitls.DimenUtil;
import gl.view.pulltofresh.PullToRefreshView;

import org.json.JSONArray;
import org.json.JSONObject;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

public class DoSearchFragment extends BaseListFragmet {

	private TextView empty;
	private ListView list;
	protected String NOTFOUND = "目前没有什么内容";
	private Drawable drawable;
	private String type;
	private String data;
	final String KEY_TYPE = "type";
	final String KEY_DATA = "data";
	private int maxID;

	private void bindViews() {
		empty = (TextView) findViewById(R.id.empty);
		list = (ListView) findViewById(android.R.id.list);
		list.setEmptyView(empty);
		empty.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				initdata();
			}
		});
		empty.setText(NOTFOUND);
		empty.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null,
				null);
	}

	public DoSearchFragment() {
	}
@Override
protected boolean getLoadMoreAble() {
	return true;
}
protected boolean getRefreAble() {
	return true;
}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.listview_empty, container, false);
		drawable = getActivity().getResources().getDrawable(
				R.drawable.ic_discovery_pic_btn3);
		bindViews();
		Bundle pars = getArguments();
		if (pars != null) {
			type = pars.getString(KEY_TYPE);
			data = pars.getString(KEY_DATA);
			if (TextUtils.isEmpty(data)) {
				data = "0";
			}
			initdata();
		}
		return rootView;
	}

	private void initdata() {
		if (getListView() instanceof ListView) {
			((ListView) getListView()).setDividerHeight((int) DimenUtil
					.dp2px(4));
		}
		// http://localhost:8080/by_webservice/GetSearchForUser?userid=162000&type=1&data=1978-01-28,1988-01-28,170,181,2,5,2,5,2
		initAbsListView(getListView(), API.URL
				+ "GetSearchForUser?userid=%s&type=%s&data=%s&maxid=%s", new Object[] {
				UserManger.getInstance().userid, type, data ,maxID},
				R.layout.item_list_image, new String[] { "username", "avatar",
						BYQUserInfo.KEY_SHENFEN, "monolog",BYQUserInfo.KEY_GROUP,BYQUserInfo.KEY_VIP_UNIT }, new int[] {
						R.id.tv_nick, R.id.iv_avator, R.id.age,
						R.id.tv_jianjie, R.id.iv_vip,R.id.imageView1,R.id.iv_sendmsg, R.id.iv_fav });

	}
	@Override
	protected String preFormatJsonData(String t) {
		try {
			JSONArray ja = new JSONObject(t).getJSONArray("data");
			for (int i = 0; i < ja.length(); i++) {
				int id = ja.getJSONObject(i).getInt("userid");
//				maxID =id>maxID?id:maxID;
				if (maxID==0) {
					maxID =id;
				}else{
					maxID =id<maxID?id:maxID;//取最小
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return BYQUserInfo.preFormatShenFen(t);
	}
	
	@Override
	public void onBGARefreshLayoutBeginRefreshing() {
		maxID = 0;
		initdata();
	}

	@Override
	public void onBGARefreshLayoutBeginLoadingMore() {
		super.onBGARefreshLayoutBeginLoadingMore();
		appendAbsListView(getListView(), API.URL
				+ "GetSearchForUser?userid=%s&type=%s&data=%s&maxid=%s", new Object[] {
				UserManger.getInstance().userid, type, data ,maxID});
	}
	@Override
	public void onHeaderRefresh(PullToRefreshView view) {
		super.onHeaderRefresh(view);
		maxID = 0;
		initdata();
	}
	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		setPullListviewAble(true);
	}
	@Override
	public void onFooterRefresh(PullToRefreshView view) {
		appendAbsListView(getListView(), API.URL
				+ "GetSearchForUser?userid=%s&type=%s&data=%s&maxid=%s", new Object[] {
				UserManger.getInstance().userid, type, data ,maxID});
	}
}